package com.itmuch.cloud;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

public class QueryParamPreFilter extends ZuulFilter{

	 private static final Logger LOGGER = LoggerFactory.getLogger(QueryParamPreFilter.class);
	@Override
	public Object run() {
		 RequestContext ctx = RequestContext.getCurrentContext();
			HttpServletRequest request = ctx.getRequest();
			
			String host=request.getRemoteHost();
			System.out.println("pre request host:"+host);
		    LOGGER.info("请求的host:{}", host);
			if (request.getParameter("foo") != null) {
			    // put the serviceId in `RequestContext`
	    		ctx.put("SERVICE_ID_KEY", request.getParameter("foo"));
	    	}
	        return null;
	}

	@Override
	public boolean shouldFilter() {
		RequestContext ctx = RequestContext.getCurrentContext();
		return !ctx.containsKey("FORWARD_TO_KEY") // a filter has already forwarded
				&& !ctx.containsKey("SERVICE_ID_KEY"); // a filter has already determined serviceId
	}

	@Override
	public int filterOrder() {
		return  1; // run before PreDecoration
	}

	@Override
	public String filterType() {
		return "pre";
	}

}
